package com.vrv.netmgr.upgrade;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Handler;
import android.util.Log;

import com.vrv.common.UrlPrefix;
import com.vrv.common.util.HttpClientToServer;
/**
 * 版本信息处理
 * @author zxb
 *
 */
public class CheckVersion {
    private static final String TAG = CheckVersion.class.getSimpleName();
    private static final boolean DEBUG = true;
	//
	public static final String PATH = "edpdmupgrade";
	private static final String VERSION_FILENAME = "version.html";
	public static final String APK_FILENAME = "EDPDMClient.apk";
	//
	public Context mContext;
	public Handler mHandler;
	//
	public CheckVersion(Context context, Handler handler){
		this.mContext = context;
		this.mHandler = handler;
	}
	
	
	/**
	 * 获取本地存储的版本信息
	 */
	public int getLocalVersionCode() {
		if (DEBUG) Log.v(TAG, "getLocalVersionCode()");
		
		int versionCode = 0;
		//
		try {
			PackageInfo info = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
			versionCode = info.versionCode;
			if (DEBUG) Log.v(TAG, "getLocalVersionCode()...versionCode = " + versionCode);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		//
		return versionCode;
	}
	
	/**
	 * 获取最新版本
	 */
	public int getRemoteVersionCode() {
		Log.v(TAG, "getRemoteVersionCode");
		int versionCode = 0;
		
		try {
			String url = getUpdateUrl();
			if (null == url) {
				return -1;
			}
			String urlAddr = url + "/" + VERSION_FILENAME;
			HttpClientToServer httpClientToServer = new HttpClientToServer();
			String response = httpClientToServer.doPost(urlAddr, null);
			
			JSONObject json = new JSONObject(response);
			if (json.has("versionCode")){
				versionCode = json.getInt("versionCode");
			}
			if (DEBUG) Log.v(TAG, "getRemoteVersionCode()...versionCode = " + versionCode);
		} catch (JSONException e) {
			e.printStackTrace();
		}

		//
		return versionCode;
	}
	
	
 	/**
	 * 下载文件
	 */
	public boolean download(String file, String version){
		if(DEBUG)Log.v(TAG, "download()");
		if (null == file || "".equals(file)) {
			return false;
		}
		//
		String url = getUpdateUrl();
		if (null == url) {
			return false;
		}
		
		LoadApk loadMain = new LoadApk(mContext, file, url, version);
		loadMain.downMain();
		//
		return true;
	}
	
	/**
	 * 比较当前版本号和最新发布版本号
	 * @param pre 当前版本号
	 * @param next 最新版本号
	 * @return true 需要更新；false 不需要更新
	 */
	public boolean compareVersion(int pre, int next){
		Log.v(TAG, "compareVersion");

		boolean b = false;
		if (pre < next) {
			b = true;
		}else {
			b = false;
		}
		//
		return b;
	}
	
	
//	/**
//	 * 主入口
//	 */
//	public boolean entry() {
//		Log.v(TAG, "entry");
//		
//		boolean ret = false;
//		
//		int local = getLocalVersionCode();
//		int remote = getRemoteVersionCode();
//		boolean b = compareVersion(local, remote);
//		if (b) {
//			new AlertDialog.Builder(mContext).setMessage("是否更新最新版本")
//			.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//				public void onClick(DialogInterface dialog, int which) {
//					String file = APK_FILENAME;
//					download(file, null);
//				}
//			})
//			.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//				public void onClick(DialogInterface dialog, int which) {
//					dialog.cancel();
//				}
//			}).show();
//		}
//		//
//		return ret;
//	}
	
	/**
	 * 获取升级url地址
	 * 待处理
	 * @return
	 */
	public String getUpdateUrl(){
		if(DEBUG)Log.v(TAG, "getUpdateUrl()");
		
		String url = "";
		url = initUrl();
		
		return url;
	}
	
	/**
	 * 链接初始化
	 */
	private String initUrl(){
		if(DEBUG) Log.v(TAG, "initUrl()");
		
		UrlPrefix urlPrefix = new UrlPrefix(mContext);
		String prefix = urlPrefix.getUrlPrefix();
		if (null == prefix) {
			return null;
		}
		String urlPath = prefix + "/" + PATH;
		
		if (DEBUG) Log.v(TAG, "initUrl()... urlPath = " + urlPath);
		
		return urlPath;
	}
	
	/**
	 * 获取升级apk文件名
	 * 待处理
	 * @return
	 */
	public String getApkName(){
		if(DEBUG)Log.v(TAG, "getApkName()");
		
		String file = "";
		file = APK_FILENAME;
		return file;
	}
}
